<template>
	<view>
		<!-- <u-navbar title="套餐" :placeholder="true" leftIconColor="#fff" :autoBack="true" :bgColor="bgColor"
			:titleStyle="titleStyle"></u-navbar> -->
			<headerinfo></headerinfo>
		<!-- <headertop :inv="5"></headertop> -->
				<backos></backos>
		<view class="form">
			<view class="userBox">
				<image :src="userData.avatar" mode="aspectFill"></image>
				<view class="label">
					{{ userData.realname?userData.realname:userData.nickname?userData.nickname:userData.username  }}
				</view>
				<view class="desc" v-if="userData.checkinRsp && userData.checkinRsp.privilegePeriodValidUntil">
					套餐到期时间：{{userData.checkinRsp.privilegePeriodValidUntil}}
				</view>
			</view>
			<view class="cardList">
				<view class="list" @click="changeInv(index)" :class="Inv == index?'active':''"
					v-for="(item,index) in tablist" :key="index">
					<view class="label">
						{{item.period == "month"?'每月':item.period == "season"?'1季度':'1年'}}
					</view>
					<view class="price">
						<span>￥</span>{{item.pay}}
					</view>
				</view>
			</view>
			<view class="qianyi">
				<view class="label">
					套餐权益：<span style="color: #de4849;font-weight: bold;">{{tablist[Inv].familyCoin}}家庭币
						{{tablist[Inv].ossSpace}}M空间</span>
				</view>
			
			</view>
			
			<view class="buy" @click="buy">
				立即购买
			</view>
		</view>
		
	</view>
</template>

<script>
	const util = require('@/common/refresh.js');
	export default {
		components: {},
		data() {
			return {
				bgColor: '#de4849',
				titleStyle: {
					color: '#fff'
				},
				Inv: 0,
				tablist: [],
				userData: {}
			};
		},
		onLoad() {
			this.getUserData();
			this.getData();
		},
		methods: {
			buy: util.throttle(function() {
				// #ifdef MP-WEIXIN
				let totalFee = this.tablist[this.Inv].pay
				let description = '充值-心之归宿会员'
				let app = this
				wx.login({
					success(res) {
						if (res.code) {
							let data = {
								code: res.code,
								description: description,
								totalFee: totalFee,
							}
							app.$api.wxMiniPayGet(data).then(params => {
								console.log(params)
								wx.requestPayment({
									timeStamp: params.timeStamp + '',
									nonceStr: params.nonceStr,
									package: 'prepay_id=' + params.prepay_id,
									signType: params.signType,
									paySign: params.paySign,
									success(res) {
										app.$Show.successJump('支付成功')
									},
									fail(res) {
										app.$Show.show('支付失败')
									
									}
								})
							})
						}
					}
				})
				// #endif
				


				// let data = {
				// 	privilegePeriod:this.tablist[this.Inv].period
				// }
				// this.$api.pay(data).then(res=>{
				// 	console.log(res)
				// 	this.$Show.successJump('购买成功')
				// })
			}),
			getUserData() {
				this.$api.myInfo().then(res => {
					this.userData = res
					console.log('用户信息')
				})
			},
			getData() {
				let arr = []
				this.$api.getPaySet().then(res => {
					console.log(res)
					arr = [res.set.month, res.set.season, res.set.year]
					this.tablist = arr
				})
			},
			changeInv(index) {
				if (this.Inv == index) {
					return
				}

				this.Inv = index
			}
		}
	};
</script>

<style lang="scss" scoped>
	.form{
		width: 600px;
		margin: 0 auto;
		    box-shadow: 0 0 3px #ccc;
		    border-radius: 10px;
			padding:20px 30px;
	}
	.qianyi {
		padding: 20rpx;
		margin-top: 30rpx;

	}

	.userBox {
		position: relative;
		padding: 20rpx;
		padding-left: 140rpx;
		margin-top: 30rpx;

		.label {
			margin-bottom: 10rpx;
		}

		.desc {
			font-size: 26rpx;
			color: #ababab;
		}

		image {
			position: absolute;
			left: 20rpx;
			top: 50%;
			transform: translateY(-50%);
			width: 100rpx;
			height: 100rpx;
			border-radius: 50px;
		}
	}

	.buy {
		width: 700rpx;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #e8e8e8;
		border-radius: 5px;
		border-color: #de4849;
		background-color: #de4849;
		color: #fff;
		margin: 0 auto;
		margin-top: 120rpx;
		cursor: pointer;
	}

	.cardList {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx;

		.list {
			width: 30%;
			background-color: #f7f9fc;
			padding: 20rpx;
			box-sizing: border-box;
			border-radius: 10rpx;
			border: 1px solid #f7f9fc;
			cursor: pointer;

			.label {
				margin-bottom: 40rpx;
				font-weight: bold;
			}

			.price {
				font-size: 40rpx;
				font-weight: bold;
				color: #de4849;

				span {
					font-size: 28rpx;
					cursor: pointer;
				}
			}

		}

		.active {
			border-color: #de4849;
		}
	}
</style>